# import vlc

from app.util.annotation_util import annotate, ClassInfo
from app.view.base_view import View


@annotate(ClassInfo(name="Video", category="muse", plug=False))
class VideoView(View):
    def __init__(self, parent, controller):
        super().__init__(parent, controller)
        # 初始化VLC实例和媒体播放器

    #     self.instance = vlc.Instance()
    #     self.media_player = self.instance.media_player_new()
    #
    #     # 播放列表相关变量
    #     self.playlist = []
    #     self.current_index = -1
    #     self.is_playing = False
    #
    #     # 创建UI
    #     self.create_ui()
    #
    #     # 绑定事件
    #     # self.protocol("WM_DELETE_WINDOW", self.on_close)
    #
    #     # 启动进度更新线程
    #     self.update_progress_thread = threading.Thread(target=self.update_progress, daemon=True)
    #     self.update_thread_running = True
    #     self.update_progress_thread.start()
    #
    # def create_ui(self):
    #     """创建用户界面"""
    #     # 主框架
    #     self.main_frame = ttk.Frame(self, padding=10)
    #     self.main_frame.pack(fill=BOTH, expand=True)
    #
    #     # 顶部控制栏
    #     self.create_control_bar()
    #
    #     # 创建分隔符
    #     ttk.Separator(self.main_frame, orient=HORIZONTAL).pack(fill=X, pady=5)
    #
    #     # 中间内容区（视频播放+播放列表）
    #     content_frame = ttk.Frame(self.main_frame)
    #     content_frame.pack(fill=BOTH, expand=True, pady=5)
    #
    #     # 视频播放区
    #     self.create_video_area(content_frame)
    #
    #     # 播放列表区
    #     self.create_playlist_area(content_frame)
    #
    #     # 创建分隔符
    #     ttk.Separator(self.main_frame, orient=HORIZONTAL).pack(fill=X, pady=5)
    #
    #     # 底部播放控制栏
    #     self.create_playback_controls()
    #
    # def create_control_bar(self):
    #     """创建顶部控制栏"""
    #     control_bar = ttk.Frame(self.main_frame)
    #     control_bar.pack(fill=X, pady=5)
    #
    #     # 选择文件夹按钮
    #     ttk.Button(
    #         control_bar,
    #         text="选择视频文件夹",
    #         command=self.select_video_folder
    #     ).pack(side=LEFT, padx=5)
    #
    #     # 清除播放列表按钮
    #     ttk.Button(
    #         control_bar,
    #         text="清除播放列表",
    #         command=self.clear_playlist
    #     ).pack(side=LEFT, padx=5)
    #
    #     # 播放模式控制
    #     self.repeat_var = tk.BooleanVar(value=False)
    #     ttk.Checkbutton(
    #         control_bar,
    #         text="循环播放",
    #         variable=self.repeat_var
    #     ).pack(side=RIGHT, padx=10)
    #
    #     self.shuffle_var = tk.BooleanVar(value=False)
    #     ttk.Checkbutton(
    #         control_bar,
    #         text="随机播放",
    #         variable=self.shuffle_var
    #     ).pack(side=RIGHT, padx=10)
    #
    # def create_video_area(self, parent):
    #     """创建视频播放区域"""
    #     video_frame = ttk.LabelFrame(parent, text="视频播放", padding=10)
    #     video_frame.pack(side=LEFT, fill=BOTH, expand=True, padx=5)
    #
    #     # 视频画布
    #     self.video_canvas = tk.Canvas(video_frame, bg="black")
    #     self.video_canvas.pack(fill=BOTH, expand=True)
    #
    #     # 将VLC播放器与Canvas关联
    #     if os.name == "nt":  # Windows系统
    #         self.media_player.set_hwnd(self.video_canvas.winfo_id())
    #     else:  # Linux/Mac系统
    #         self.media_player.set_xwindow(self.video_canvas.winfo_id())
    #
    #     # 视频信息
    #     info_frame = ttk.Frame(video_frame)
    #     info_frame.pack(fill=X, pady=5)
    #
    #     self.video_title = ttk.Label(info_frame, text="未播放视频", font=("Arial", 12, "bold"))
    #     self.video_title.pack(anchor=W)
    #
    # def create_playlist_area(self, parent):
    #     """创建播放列表区域"""
    #     playlist_frame = ttk.LabelFrame(parent, text="播放列表", padding=10)
    #     playlist_frame.pack(side=RIGHT, fill=BOTH, expand=True, padx=5, ipadx=5)
    #
    #     # 播放列表
    #     self.playlist_box = tk.Listbox(
    #         playlist_frame,
    #         selectmode=SINGLE,
    #         # bg=self.cget("bg"),
    #         # fg=self.cget("fg"),
    #         selectbackground=ttk.Style().lookup("TButton", "background"),
    #         selectforeground=ttk.Style().lookup("TButton", "foreground")
    #     )
    #     self.playlist_box.pack(fill=BOTH, expand=True)
    #     self.playlist_box.bind('<<ListboxSelect>>', self.on_playlist_select)
    #     self.playlist_box.bind('<Double-1>', self.on_playlist_double_click)
    #
    #     # 添加滚动条
    #     scrollbar = ttk.Scrollbar(
    #         playlist_frame,
    #         orient=VERTICAL,
    #         command=self.playlist_box.yview
    #     )
    #     scrollbar.pack(side=RIGHT, fill=Y)
    #     self.playlist_box.config(yscrollcommand=scrollbar.set)
    #
    # def create_playback_controls(self):
    #     """创建底部播放控制栏"""
    #     control_frame = ttk.LabelFrame(self.main_frame, text="播放控制", padding=10)
    #     control_frame.pack(fill=X, pady=5)
    #
    #     # 进度条
    #     progress_frame = ttk.Frame(control_frame)
    #     progress_frame.pack(fill=X, pady=5)
    #
    #     self.time_label = ttk.Label(progress_frame, text="00:00/00:00")
    #     self.time_label.pack(side=LEFT, padx=5)
    #
    #     self.progress_var = tk.DoubleVar()
    #     self.progress_bar = ttk.Scale(
    #         progress_frame,
    #         variable=self.progress_var,
    #         from_=0,
    #         to=100,
    #         orient=HORIZONTAL,
    #         command=self.set_position
    #     )
    #     self.progress_bar.pack(side=LEFT, fill=X, expand=True, padx=5)
    #
    #     # 控制按钮
    #     button_frame = ttk.Frame(control_frame)
    #     button_frame.pack(fill=X, pady=5)
    #
    #     # 快退按钮
    #     ttk.Button(
    #         button_frame,
    #         text="快退",
    #         command=lambda: self.seek(-10)  # 快退10秒
    #     ).pack(side=LEFT, padx=10)
    #
    #     # 上一首按钮
    #     ttk.Button(
    #         button_frame,
    #         text="上一个",
    #         command=self.play_previous
    #     ).pack(side=LEFT, padx=10)
    #
    #     # 播放/暂停按钮
    #     self.play_pause_btn = ttk.Button(
    #         button_frame,
    #         text="播放",
    #         command=self.toggle_play_pause,
    #         bootstyle=SUCCESS
    #     )
    #     self.play_pause_btn.pack(side=LEFT, padx=10)
    #
    #     # 下一首按钮
    #     ttk.Button(
    #         button_frame,
    #         text="下一个",
    #         command=self.play_next
    #     ).pack(side=LEFT, padx=10)
    #
    #     # 快进按钮
    #     ttk.Button(
    #         button_frame,
    #         text="快进",
    #         command=lambda: self.seek(10)  # 快进10秒
    #     ).pack(side=LEFT, padx=10)
    #
    #     # 音量控制
    #     ttk.Label(button_frame, text="音量:").pack(side=RIGHT, padx=5)
    #     self.volume_var = tk.DoubleVar(value=100)
    #     self.volume_scale = ttk.Scale(
    #         button_frame,
    #         variable=self.volume_var,
    #         from_=0,
    #         to=100,
    #         orient=HORIZONTAL,
    #         command=self.set_volume
    #     )
    #     self.volume_scale.pack(side=RIGHT, fill=X, expand=True, padx=5, ipady=5)
    #     self.media_player.audio_set_volume(100)
    #
    # def select_video_folder(self):
    #     """选择视频文件夹并加载视频文件"""
    #     folder = filedialog.askdirectory()
    #     if folder:
    #         # 扫描文件夹中的视频文件
    #         video_extensions = ['.mp4', '.avi', '.mkv', '.mov', '.flv', '.wmv', '.rmvb', '.mpg']
    #         for root, dirs, files in os.walk(folder):
    #             for file in files:
    #                 if any(file.lower().endswith(ext) for ext in video_extensions):
    #                     file_path = os.path.join(root, file)
    #                     if file_path not in self.playlist:
    #                         self.playlist.append(file_path)
    #                         self.playlist_box.insert(END, os.path.basename(file_path))
    #
    # def clear_playlist(self):
    #     """清除播放列表"""
    #     self.playlist.clear()
    #     self.playlist_box.delete(0, END)
    #     self.stop_playback()
    #     self.current_index = -1
    #     self.video_title.config(text="未播放视频")
    #
    # def on_playlist_select(self, event):
    #     """播放列表选择事件"""
    #     selection = self.playlist_box.curselection()
    #     if selection:
    #         self.current_index = selection[0]
    #
    # def on_playlist_double_click(self, event):
    #     """双击播放列表项播放视频"""
    #     selection = self.playlist_box.curselection()
    #     if selection:
    #         self.current_index = selection[0]
    #         self.play_selected()
    #
    # def play_selected(self):
    #     """播放选中的视频"""
    #     if 0 <= self.current_index < len(self.playlist):
    #         video_path = self.playlist[self.current_index]
    #         self.load_and_play_video(video_path)
    #
    # def load_and_play_video(self, video_path):
    #     """加载并播放视频"""
    #     try:
    #         # 停止当前播放
    #         self.media_player.stop()
    #
    #         # 加载新视频
    #         media = self.instance.media_new(video_path)
    #         self.media_player.set_media(media)
    #
    #         # 播放视频
    #         self.media_player.play()
    #         self.is_playing = True
    #         self.play_pause_btn.config(text="暂停")
    #
    #         # 更新视频标题
    #         self.video_title.config(text=os.path.basename(video_path))
    #
    #         # 高亮显示当前播放项
    #         self.playlist_box.selection_clear(0, END)
    #         self.playlist_box.selection_set(self.current_index)
    #         self.playlist_box.see(self.current_index)
    #
    #     except Exception as e:
    #         print(f"播放错误: {e}")
    #         ttk.dialogs.Messagebox.showerror("播放错误", f"无法播放视频: {str(e)}")
    #
    # def toggle_play_pause(self):
    #     """切换播放/暂停状态"""
    #     if not self.playlist:
    #         return
    #
    #     if self.is_playing:
    #         # 暂停播放
    #         self.media_player.pause()
    #         self.is_playing = False
    #         self.play_pause_btn.config(text="播放")
    #     else:
    #         # 恢复播放或开始播放
    #         if self.current_index == -1 and self.playlist:
    #             self.current_index = 0
    #             self.load_and_play_video(self.playlist[self.current_index])
    #         else:
    #             self.media_player.play()
    #             self.is_playing = True
    #             self.play_pause_btn.config(text="暂停")
    #
    # def play_previous(self):
    #     """播放上一首"""
    #     if not self.playlist:
    #         return
    #
    #     if self.shuffle_var.get():
    #         # 随机播放模式
    #         if len(self.playlist) > 1:
    #             new_index = self.current_index
    #             while new_index == self.current_index:
    #                 new_index = random.randint(0, len(self.playlist) - 1)
    #             self.current_index = new_index
    #             self.play_selected()
    #     else:
    #         # 顺序播放模式
    #         self.current_index = (self.current_index - 1) % len(self.playlist)
    #         self.play_selected()
    #
    # def play_next(self):
    #     """播放下一首"""
    #     if not self.playlist:
    #         return
    #
    #     if self.shuffle_var.get():
    #         # 随机播放模式
    #         if len(self.playlist) > 1:
    #             new_index = self.current_index
    #             while new_index == self.current_index:
    #                 new_index = random.randint(0, len(self.playlist) - 1)
    #             self.current_index = new_index
    #             self.play_selected()
    #     else:
    #         # 顺序播放模式
    #         self.current_index = (self.current_index + 1) % len(self.playlist)
    #         self.play_selected()
    #
    # def seek(self, seconds):
    #     """快进或快退指定秒数"""
    #     if self.media_player.is_playing():
    #         current_time = self.media_player.get_time()
    #         new_time = current_time + (seconds * 1000)  # VLC使用毫秒
    #         self.media_player.set_time(new_time)
    #
    # def set_volume(self, value):
    #     """设置音量"""
    #     volume = int(float(value))
    #     self.media_player.audio_set_volume(volume)
    #
    # def set_position(self, value):
    #     """设置播放位置"""
    #     if self.media_player.is_playing():
    #         position = float(value) / 100.0
    #         self.media_player.set_position(position)
    #
    # def format_time(self, milliseconds):
    #     """格式化时间（毫秒 -> mm:ss）"""
    #     seconds = milliseconds // 1000
    #     minutes = seconds // 60
    #     seconds %= 60
    #     return f"{minutes:02d}:{seconds:02d}"
    #
    # def update_progress(self):
    #     """更新播放进度"""
    #     while self.update_thread_running:
    #         if self.media_player.is_playing():
    #             try:
    #                 # 获取当前播放时间和总时长
    #                 current_time = self.media_player.get_time()
    #                 total_time = self.media_player.get_length()
    #
    #                 if total_time > 0:
    #                     # 更新进度条
    #                     progress = (current_time / total_time) * 100
    #                     self.progress_var.set(progress)
    #
    #                     # 更新时间标签
    #                     time_text = f"{self.format_time(current_time)}/{self.format_time(total_time)}"
    #                     self.time_label.config(text=time_text)
    #             except:
    #                 pass
    #
    #         # 检查视频是否播放完毕
    #         if not self.media_player.is_playing() and self.is_playing:
    #             if self.repeat_var.get() and self.current_index != -1:
    #                 # 单曲循环
    #                 self.play_selected()
    #             elif self.current_index != -1 and len(self.playlist) > 0:
    #                 # 播放下一首
    #                 self.play_next()
    #             else:
    #                 # 播放结束
    #                 self.is_playing = False
    #                 self.play_pause_btn.config(text="播放")
    #
    #         time.sleep(0.5)
    #
    # def stop_playback(self):
    #     """停止播放"""
    #     self.media_player.stop()
    #     self.is_playing = False
    #     self.play_pause_btn.config(text="播放")
    #     self.progress_var.set(0)
    #     self.time_label.config(text="00:00/00:00")
    #
    # def on_close(self):
    #     """关闭窗口时的处理"""
    #     self.update_thread_running = False
    #     self.media_player.stop()
    #     self.destroy()

    def create_widgets(self):
        pass
